package com.jwaoo.chat.ui;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import com.jwaoo.chat.R;

/**
 * Created by jwaoo on 2017/8/31.
 */

public class SwipeLinerLayout extends LinearLayout{
    private float maxDex;
    private boolean isEnd = false;//是否滑到最左边
    TranslateAnimation translateAnimation ;
    public SwipeLinerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        maxDex = getResources().getDimensionPixelSize(R.dimen.swipe_w);
    }
    private float startX = 0;
    private float dex = 0;
    private float proX = 0;
    private boolean isMove = false;
    private float right = 0;
    private boolean [] lock = new boolean[2];//lock[0] : down事件发生，lock[1] :up事件发生
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //当目标view在初始位置时，默认动画结束（解决动画没有执行完，view就被销毁的情况）
        //
        if (!isAnimationStart || getLeft() == 0){
            if (isEnd && getLeft() != 0) {
                doSwipeToStart();
            }else if (getLeft() == 0){
                isEnd = false;
                dex = 0;
                isAnimationStart = false;
            }

            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    startX = event.getRawX();
                    isMove = false;
                    right = getRight();
                    lock[0] = false;
                    lock[1] = false;
                    return true;
                case MotionEvent.ACTION_MOVE:
                    if (!lock[1]){
                        proX = event.getRawX();
                        float d = (proX - startX)/2;
                        if (!(dex >= 0 && d > 0)){
                            startX = proX;
                            dex += Math.round(d);
                            if (dex < (-maxDex)){
                                layout((int)(-maxDex),getTop(),(int)(getWidth()-maxDex),getBottom());
                                dex = -maxDex;
                            }else if (dex <= 0 && dex > (-maxDex)){
                                layout((int)(dex),getTop(),(int)(right+dex),getBottom());
                            }else if(dex > 0 && isEnd){
                                layout(0,getTop(),getWidth(),getBottom());
                            }
                            postInvalidate();
                            if (Math.abs(d) > 1){
                                isMove = true;
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (!isMove) return false;
                    if (!isAnimationStart){
                        if (!isEnd){
                            swipe(isEnd,Math.abs(dex)-maxDex);
                            dex = -maxDex;
                            isEnd = true;
                        }else {
                            doSwipeToStart();
                            dex = 0;
                            isEnd = false;
                        }
                    }
                    break;
            }

        }
        return super.onTouchEvent(event);
    }

    public boolean isExpand(){
        return isEnd;
    }

    public void doSwipeToStart(){
        isAnimationStart = true;
        swipe(isEnd,-dex);
        dex = 0;
    }

    private boolean isAnimationStart = false;
    private void swipe(final boolean end,float endX){
        translateAnimation = new TranslateAnimation(0,endX,0,0);
        translateAnimation.setInterpolator(new LinearInterpolator());
        translateAnimation.setDuration(100);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isAnimationStart = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (!end){
                    layout((int)(-maxDex),getTop(),(int)(getWidth()-maxDex),getBottom());
                }else {
                    layout(0,getTop(),getWidth(),getBottom());
                    isEnd = false;
                }
                postInvalidate();
                clearAnimation();
                if (!lock[0]) lock[1] = true;
                isAnimationStart = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(translateAnimation);
    }

    @Override
    public void computeScroll() {
    }
}
